<?php

require_once(SERVER_ROOT . '/daos/' . 'UsuarioDAO.php');
require_once(SERVER_ROOT . '/daos/' . 'EscritorioDAO.php');

class AdminController {

    private $usuarioDAO;
    private $escritorioDAO;

    public function __construct() {
        $this->usuarioDAO = new UsuarioDAO();
        $this->escritorioDAO = new EscritorioDAO();
    }

    public function listAllByType($p) {

        $page = $p['page'];
        $limit = $p['rows'];
        $sidx = $p['sidx'];
        $sord = $p['sord'];
        
        $search_field = (isset($p['searchField']))?$p['searchField'] : "";
        $search_string = (isset($p['searchString']))?$p['searchString'] : "";
        $search_oper = (isset($p['searchOper']))?$p['searchOper'] : "";
        if (!$sidx)
            $sidx = 1;
        $count = $this->usuarioDAO->countByType($p['type']);
        $total_pages = ($count > 0 ? ceil($count / $limit) : $total_pages = 0);
        if ($page > $total_pages)
            $page = $total_pages;
        $start = $limit * $page - $limit;
        if ($start < 0)
            $start = 0;
        if ($search_string == "") {
            $result = $this->usuarioDAO->findAllByType($p['type'], $sidx, $sord, $start, $limit);
        } else {
            $result = $this->usuarioDAO->findAllByTypeWithFilters($p['type'], $sidx, $sord, $start, $limit, $search_field, $search_string, $search_oper);
        }

        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $count;
        for ($i = 0; $i < count($result); $i++) {
            $row = $result[$i];
            $response->rows[$i]['id'] = $row['id_usuario'];
            $response->rows[$i]['cell'] = array($row['id_usuario'], $row['id_escritorio'], $row['nome'], $row['endereco'], $row['cidade'], $row['cep'], $row['telefone_fixo'], $row['telefone_movel'], $row['email'], $row['estado'], $row['rg'], $row['cpf'], $row['data_nascimento'], $row['status'], '');
        }
        echo json_encode($response);
    }

    public function listarTodosUsuariosPendentes() {
        $usuarios = $this->usuarioDAO->findAllStatusPendency();
        $json = json_encode($usuarios);
        // 		print $json;
        return $json;
    }

    public function listarUsuariosPendentesPeloTipo(array $params) {
        $tipo = $params['tipo_usuario'];
        $usuarios = $this->usuarioDAO->findStatusPendencyByUserType($tipo);
        $json = json_encode($usuarios);
        return $json;
    }

    public function removerUsuario(array $params) {
        $rows = $this->usuarioDAO->removeById($params['id']);
        if ($rows) {
            echo "ok";
        } else {
            echo "fail";
        }
    }

    public function listAllOffice(array $p) {
        $page = $p['page'];
        $limit = $p['rows'];
        $sidx = $p['sidx'];
        $sord = $p['sord'];
        if (!$sidx)
            $sidx = 1;
        $result = $this->escritorioDAO->findAll();
        $count = count($result);
        $total_pages = ($count > 0 ? ceil($count / $limit) : $total_pages = 0);
        if ($page > $total_pages)
            $page = $total_pages;
        $start = $limit * $page - $limit;
        if ($start < 0)
            $start = 0;
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $count;
        for ($i = 0; $i < count($result); $i++) {
            $row = $result[$i];

            $response->rows[$i]['id'] = $row->id_escritorio;
            $response->rows[$i]['cell'] = array($row['id_usuario'], $row->id_escritorio, $row->cod_escritorio, $row->cod_gerente, $row->id_gerente);
        }
        echo json_encode($response);
    }

    public function getUsuario(array $params) {

        $row = $this->usuarioDAO->findById($params['id']);

        $response = array(
            $row['id_usuario'],
            $row['id_escritorio'],
            $row['id_tipo_usuario'],
            $row['login'],
            $row['senha'],
            $row['nome'],
            $row['endereco'],
            $row['cidade'],
            $row['cep'],
            $row['telefone_fixo'],
            $row['telefone_movel'],
            $row['email'],
            $row['data_nascimento'],
            $row['estado'],
            $row['rg'],
            $row['cpf'],
            $row['status']
        );
        echo json_encode($response);
    }

    public function atualizarUsuario(array $params) {
        $rows = $this->usuarioDAO->update($params);
        if ($rows) {
            echo "ok";
        } else {
            echo "fail";
        }
    }

    public function ativarUsuario(array $usuario) {
        $this->usuarioDAO->activateUserById($usuario['id_usuario']);
    }

    public function inativarUsuario(array $usuario) {
        $this->usuarioDAO->inactivateUserById($usuario['id_usuario']);
    }

}
